import winston from 'winston';
import { format } from 'winston';

// 扩展 Logger 接口以包含 silent 属性
interface CustomLogger extends winston.Logger {
  silent: boolean;
}

// 创建自定义格式
const customFormat = format.combine(
  format.timestamp({
    format: 'YYYY-MM-DD HH:mm:ss'
  }),
  format.errors({ stack: true }),
  format.splat(),
  format.json()
);

// 创建 logger 实例
export const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || 'info',
  format: customFormat,
  transports: [
    new winston.transports.Console({
      format: format.combine(
        format.colorize(),
        format.simple()
      )
    })
  ]
}) as CustomLogger;

// 如果是测试环境，默认禁用日志
if (process.env.NODE_ENV === 'test') {
  logger.silent = true;
}

// 导出日志级别类型
export type LogLevel = 'error' | 'warn' | 'info' | 'http' | 'debug';

// 扩展 logger 方法
export const error = (message: string, metadata?: Record<string, unknown>) => logger.error(message, metadata);
export const warn = (message: string, metadata?: Record<string, unknown>) => logger.warn(message, metadata);
export const info = (message: string, metadata?: Record<string, unknown>) => logger.info(message, metadata);
export const http = (message: string, metadata?: Record<string, unknown>) => logger.http(message, metadata);
export const debug = (message: string, metadata?: Record<string, unknown>) => logger.debug(message, metadata);
